﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace RMSM
{
    [Serializable()]
    public class KBYLenhBOCollection : Csla.BusinessListBase<KBYLenhBOCollection, KBYLenhBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            KBYLenhBO _KBYLenhBO = KBYLenhBO.NewChildKBYLenhBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_KBYLenhBO));
            Add(_KBYLenhBO);
            _KBYLenhBO.STT0 = this.Count;
            return _KBYLenhBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private KBYLenhBOCollection()
        {
        }

        public static KBYLenhBOCollection NewKBYLenhBOCollection()
        {
            return new KBYLenhBOCollection();
        }

        public static KBYLenhBO NewKBYLenhBO()
        {
            return KBYLenhBO.NewChildKBYLenhBO();
        }

        public static KBYLenhBOCollection GetKBYLenhBOCollection()
        {
            SPParam spParam = new SPParam("KBYLenhID", "ASC", "WHERE Deleted=0");
            return DataPortal.Fetch<KBYLenhBOCollection>(spParam);
        }

        public static KBYLenhBOCollection GetKBYLenhBOCollection(string where)
        {
            SPParam spParam = new SPParam("KBYLenhID", "ASC", "WHERE Deleted=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<KBYLenhBOCollection>(spParam);
        }

        public static KBYLenhBOCollection GetKBYLenhBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "KBYLenhID" : orderByColumn), "ASC", "WHERE Deleted=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<KBYLenhBOCollection>(spParam);
        }

        public static KBYLenhBOCollection GetKBYLenhBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "KBYLenhID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE Deleted=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<KBYLenhBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBYLenhBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                int stt = 0;
                while (dr.Read())
                {
                    KBYLenhBO objKBYLenhBO = KBYLenhBO.GetChildKBYLenhBO(dr);
                    objKBYLenhBO.STT0 = ++stt;
                    this.Add(objKBYLenhBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            RaiseListChangedEvents = false;
            foreach (KBYLenhBO child in this)
            {
                if (child.IsNew && !StaticUtil.GetBool(child.Deleted))
                    child.Insert();
                else
                    child.Update();
            }
            RaiseListChangedEvents = true;
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public KBYLenhBO GetKBYLenhBO(int _KBYLenhID)
        {
            KBYLenhBO returnObject = null;
            foreach (KBYLenhBO _KBYLenhBO in this)
            {
                if (_KBYLenhBO.KBYLenhID == _KBYLenhID)
                {
                    returnObject = _KBYLenhBO;
                    break;
                }
            }
            return returnObject;
        }

        public void Remove(int KBYLenhID)
        {
            foreach (KBYLenhBO _KBYLenhBO in this)
            {
                if (_KBYLenhBO.KBYLenhID == KBYLenhID)
                {
                    try
                    {
                        this.Remove(_KBYLenhBO);
                    }
                    catch { }
                    break;
                }
            }
        }

        public void SortByStt()
        {
            SortingByStt(0, this.Count - 1);
        }

        private void SortingByStt(int beg, int end)
        {
            if (end <= beg)
                return;
            else
            {
                int pivot = GetPivotPointByStt(beg, end);
                if (pivot > beg)
                    SortingByStt(beg, pivot - 1);
                if (pivot < end)
                    SortingByStt(pivot + 1, end);
            }
        }

        private int GetPivotPointByStt(int begPoint, int endPoint)
        {
            int pivot = begPoint;
            int m = begPoint + 1;
            int n = endPoint;
            int sttPivot = this[pivot].Stt.Value;
            int sttM = this[m].Stt.Value;
            int sttN = this[n].Stt.Value;
            KBYLenhBO temp = null;

            while (m < endPoint && sttPivot >= sttM)
            {
                m++;
                sttM = this[m].Stt.Value;
            }

            while (n > begPoint && sttPivot <= sttN)
            {
                n--;
                sttN = this[n].Stt.Value;
            }

            while (m < n)
            {
                temp = this[m].Clone();
                this[m] = this[n];
                this[n] = temp;
                sttM = this[m].Stt.Value;
                sttN = this[n].Stt.Value;

                while (m < endPoint && sttPivot >= sttM)
                {
                    m++;
                    sttM = this[m].Stt.Value;
                }

                while (n > begPoint && sttPivot <= sttN)
                {
                    n--;
                    sttN = this[n].Stt.Value;
                }
            }

            if (pivot != n)
            {
                temp = this[n].Clone();
                this[n] = this[pivot];
                this[pivot] = temp;
            }

            return n;
        }
        #endregion Business Method
        //==========================================================================
    }
}
